package com.qh.servlet;

import com.google.gson.Gson;
import com.qh.dao.Jdbc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * com.qh.servlet
 * 26832
 * yjj
 * &#064;description: 结果详情
 * &#064;author: lxm
 * ResultServlet
 * &#064;create: 2024-06-08 07:04
 **/
@WebServlet("/detel")
public class ResultServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询result表，展示结果
        //查询用户的基本信息
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String idcard = (String) session.getAttribute("idcard");
        Date date = new Date();
        String sql = "select * from resoult where idcard='"+idcard+"' order by id desc limit 1";
        Connection con = null;
        Statement smt = null;
        ResultSet rs = null;
        Jdbc jdbc = new Jdbc();
        String r1 = null;
        String r2 = null;
        String r3 = null;
        String dr1 = null;
        String dr2 = null;
        String dr3 = null;
        try{
            con = jdbc.getConnection();
            smt = con.createStatement();
            rs = smt.executeQuery(sql);
            while(rs.next()){
                date.setIdcard(rs.getString("idcard"));
                r1 = rs.getString("resoult1");
                dr1 = rs.getString("resoult1data");
                r2 = rs.getString("resoult2");
                dr2 = rs.getString("resoult2data");
                r3 = rs.getString("resoult3");
                dr3 = rs.getString("resoult3data");
                date.setDoctor_name(rs.getString("doctor"));
            }
            //组合结果限定词
            double dd1 = dr1 != null ? Double.parseDouble(dr1) : 0.0;
            double dd2 = dr2 != null ? Double.parseDouble(dr2) : 0.0;
            double dd3 = dr3 != null ? Double.parseDouble(dr3) : 0.0;

            String d1 = null;
            String d2 = null;
            String d3 = null;
            if(dd1>=0.9){
                d1 = "极有可能患有:";
            }else if(dd1>=0.6&&dd1<0.9){
                d1 = "很有可能患有:";
            }else{
                d1 = "可能患有:";
            }
            if(dd2>=0.9){
                d2 = "极有可能患有:";
            }else if(dd2>=0.6&&dd2<0.9){
                d2 = "很有可能患有:";
            }else{
                d2 = "可能患有:";
            }
            if(dd3>=0.9){
                d3 = "极有可能患有:";
            }else if(dd3>=0.6&&dd3<0.9){
                d3 = "很有可能患有:";
            }else{
                d3 = "可能患有:";
            }
            date.setDoctor_specialty(d1+r1+" "+d2+r2+" "+d3+r3+"以上结果仅供参考");
            String sql1 = "select * from users where idcard='"+idcard+"'";
            rs = smt.executeQuery(sql1);
            while(rs.next()){
                date.setUname(rs.getString("uname"));
                date.setSex(rs.getString("sex"));
                date.setAge(rs.getInt("age"));
            }
            jdbc.close(con,smt,rs);
            //转化为json数据发送给fetch
            Gson gson = new Gson();
            String json = gson.toJson(date);
            System.out.println(json);
            resp.getWriter().write(json);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                jdbc.close(con,smt,rs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
class Date{
    String uname;
    String sex;
    int age;
    String idcard;
    String doctor_name;
    String doctor_specialty;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public String getDoctor_name() {
        return doctor_name;
    }

    public void setDoctor_name(String doctor_name) {
        this.doctor_name = doctor_name;
    }

    public String getDoctor_specialty() {
        return doctor_specialty;
    }

    public void setDoctor_specialty(String doctor_specialty) {
        this.doctor_specialty = doctor_specialty;
    }
}
